This functions allows to expand a template with an expansion form in a given recursion level. In each level of expansion each template symbol is replaced by the expansion pattern, which is transposed to begin at the current template symbol. In the first level the pattern (a b a) is transposed to positions a and d.
(gen-expansion 1 '(a d) '(a b a))
--> (a b a d e d)
In the second level the pattern (a b a) is transposed to all positions of the already expanded template (a b a d e d).
(gen-expansion 2 '(a d) '(a b a))
--> (a b a b c b a b a d e d e f e d e d)
If you look closely you'll notice the operation:
(a b a d e d)
(a b a b c b a b a d e d e f e d e d)
Try visualizing the following expansions. They make nice fractals.
(gen-expansion 5 '(a b a) '(a b a))
(gen-expansion 5 '(a c a) '(a c a))
(gen-expansion 5 '(a d a) '(a d a))
(gen-expansion 5 '(a e a) '(a e a))
(gen-expansion 5 '(a f a) '(a f a))
NOTE: You are not allowed to use transpose symbols or rest symbols as input patterns: